﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KD.Sms.UI_Sys
{
    public partial class frmUserEdit : Form
    {
        public bool E_Add = true;
        private string _strSamplingId;
        public frmUserEdit()
        {
            InitializeComponent();
        }

        public frmUserEdit(DataGridViewRow gr)
        {
            InitializeComponent();
            E_Add = false;
            _strSamplingId = gr.Cells["User_Id"].Value.ToString();
            txtChinese.Text = gr.Cells["Chinese_name"].Value.ToString();
            txtAcct.Text = gr.Cells["User_Acct"].Value.ToString();
            txtPwd.Text = DesEncrypt.Decrypt(gr.Cells["User_Pwd"].Value.ToString());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmUserEdit_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tools_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            if (e.Tool.Key == "Save")
            {
                if (string.IsNullOrEmpty(txtChinese.Text.Trim()))
                {
                    UI.MsgBox.Show(UI.MsgType.Waring, "中文名不能为空!");
                    txtChinese.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtAcct.Text.Trim()))
                {
                    UI.MsgBox.Show(UI.MsgType.Waring, "登录账号不能为空!");
                    txtAcct.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
                {
                    UI.MsgBox.Show(UI.MsgType.Waring, "密码不能为空!");
                    txtPwd.Focus();
                    return;
                }

                bool _bResult = false;
                if (E_Add)
                {
                    _bResult = DAL.SqlLite.Main.Instance.DoAddUser(txtChinese.Text, txtAcct.Text, DesEncrypt.Encrypt(txtPwd.Text));
                }
                else
                    _bResult = DAL.SqlLite.Main.Instance.DoUpdateUser(_strSamplingId, txtChinese.Text, txtAcct.Text, DesEncrypt.Encrypt(txtPwd.Text));
                if (_bResult)
                {
                    DAL.SqlLite.Main.Instance.LogWrite(SysLogType.Login, string.Format("成功{0}用户[{1}]", E_Add ? "添加" : "修改", txtChinese.Text));

                    UI.MsgBox.Show(UI.MsgType.Information, "操作成功!");
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                    UI.MsgBox.Show(UI.MsgType.Error, "操作失败!");
            }
            if (e.Tool.Key == "Cannel")
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
